package com.appspot.thinkhea;

import java.util.Vector;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.appspot.thinkhea.model.MyDBHelper;
import com.appspot.thinkhea.model.Record;
import com.appspot.thinkhea.view.RecordAdapter;

public class PanelListRecord extends Activity {
	/** Called when the activity is first created. */
	private Vector<Record> c;
	private ListView recordListView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setBackgroundDrawableResource(R.drawable.bg);
		setTitle(R.string.desc_record_list);
		setContentView(R.layout.panel_list_record);

		// Add Button Listener
		Button b = (Button) findViewById(R.id.BtnGoBack);
		b.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent myIntent = new Intent(PanelListRecord.this,
						PictureCounter.class);
				PanelListRecord.this.startActivity(myIntent);
			}
		});

		// Add Button Listener
		Button c = (Button) findViewById(R.id.BtnAddRecord);
		c.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent myIntent = new Intent(PanelListRecord.this,
						PanelSelectPlayerForRecord.class);
				PanelListRecord.this.startActivity(myIntent);
			}
		});
		// handle list event and model
		recordListView = (ListView) findViewById(R.id.RecordListView);
		recordListView.setFocusable(true);
		recordListView.setItemsCanFocus(true);
		recordListView.setClickable(true);
		recordListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		recordListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> a, View v, int position,
					long id) {
				Record r = (Record) a.getAdapter().getItem(position);

				Bundle bundle = new Bundle();
				bundle.putString("RECORD_ID", r.getId() + "");
				Intent myIntent = new Intent(PanelListRecord.this,
						PanelGridPlayerRecord.class);
				myIntent.putExtras(bundle);
				PanelListRecord.this.startActivity(myIntent);

			}
		});
		loadRecord();
	}

	public void loadRecord() {
		// load data from DB
		if (c != null) {
			c.clear();
		}
		MyDBHelper db = new MyDBHelper(this);
		db.open();
		c = db.getAllRecords();
		db.close();
		recordListView.setAdapter(new RecordAdapter(this, c));

	}
}